package com.rigi.interceptor;

import com.alibaba.fastjson.JSON;
import com.rigi.common.BaseContext;
import com.rigi.common.R;
import com.rigi.entity.Employee;
import com.rigi.entity.User;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 登陆拦截器
 */
public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        Employee employee = (Employee)request.getSession().getAttribute("employee");

        if (employee!=null){
            BaseContext.setCurrentId(employee.getId());
            return true;
        }
        User user = (User) request.getSession().getAttribute("user");
        if (user!=null){
           BaseContext.setCurrentId(user.getId());
            return true;
        }
        response.getWriter().write(JSON.toJSONString(R.error("NOTLOGIN")));
        return false;
    }
}
